<?php
class ArticleAction extends Action {

    public function _initialize() {
        $Setting = M('Setting');
        $setting = $Setting->find(1);
        
        $this->assign('setting', $setting);
    }
    
    public function entry() {
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
        }
        $Article = M('Article');
        $detail = $Article->find($id);
        $detail['hits'] = $detail['hits'] + 1;
        $Article->save($detail);
        // $other = $Article->where(array('cat_id' => $detail['cat_id']))->order(array('article_id' => 'asc'))->limit(8)->select();
        $news = $Article->where(array('cat_id' => $detail['cat_id']))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit('1,5')->select();
        $this->assign('news', $news);
        
        $Category = M('category');
        $category = $Category->find($detail['cat_id']);
        
        $this->assign('detail', $detail);
        // $this->assign('other', $other);
        $this->assign('category', $category);
        
        /* 调用评论 */
        $Comment = M('Comment');
        
        import("ORG.Util.Page");
        $count = $Comment->where(array('article_id' => $id, 'status' => 1))->count();
        $Page = new Page($count, 5);
        $show = $Page->show();
        $this->assign('show', $show);
        
        $comment = $Comment->where(array('article_id' => $id, 'status' => 1))->order(array('add_time' => 'desc', 'comment_id' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $this->assign('comment', $comment);
        
        $this->display();
    }
    
    public function category() {
    	if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
	    	$Article = M('Article');
            import("ORG.Util.Page");
            import("ORG.Util.PageNav");

            $count = $Article->where(array('cat_id' => $id))->count();
            $Page = new PageNav($count, 25);
            $Page->setConfig('theme', '<li>%upPage%</li> <li id="n_num">%nowPage%/%totalPage%</li> <li>%downPage%</li>');
            $show = $Page->show();
            $this->assign('page',$show);
            
	        $article = $Article->where(array('cat_id' => $id))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
            
            $Category = M('Category');
            $category = $Category->find($id);
            
            $this->assign('article', $article);
            $this->assign('category', $category);
            
            // $other = $Article->where(array('cat_id' => $id))->order(array('article_id' => 'asc'))->limit(8)->select();
            // $this->assign('other', $other);
            $news = $Article->where(array('cat_id' => 13))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit(5)->select();
            $this->assign('news', $news);
            
	        $this->display();
        } else {
            $this->error();
        }
    }
    
    /* 插入文章评论 */
    public function insertComment() {
        $Comment = D('Comment');
        if ($Comment->create()) {
            $Comment->author_ip = getIP();
            if ($Comment->add()) {
                $this->success('成功发表评论，请耐心等待审核！');
            } else {
                $this->error('发表评论失败！');
            }
        } else {
            $this->error($Comment->getError());
        }
    }
    
}
?>